// ClientManager.h: interface for the ClientManager class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_SERVERMANAGER_H__4A49B5A1_053C_422B_ACBF_31E0091A5EBC__INCLUDED_)
#define AFX_SERVERMANAGER_H__4A49B5A1_053C_422B_ACBF_31E0091A5EBC__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <winsock2.h>
#include <ws2tcpip.h>
#include <wspiapi.h>
#include "../Release/udt.h"
#include "EntityDefine.h"
#include "AFXTEMPL.H"
#include "DBDataManager.h"

class CClientManager
{
private:
	int 	m_ServerSendTryCount;
	CString	m_strLastError;
	static CClientManager* m_pInstance; 
	UDTSOCKET m_client;
	struct addrinfo *peer;
	BOOL	m_bIsRunning;
	BOOL	m_bIsInit;
	CArray<lpCommandInfo,lpCommandInfo> m_arrTaskList;
	HANDLE	m_hEventTaskInfo;
	SocketStateChanged m_SocketCB;
	pAccountReviced m_RevicedCB;
	void* m_pParent;
	inline void SocketStateChangedCB(void* pSocket,int iType)
	{
		if(m_SocketCB!=NULL)
		{
			m_SocketCB(m_pParent,pSocket,iType);
		}
	};
	inline void PostRevicedCB(NetExchangeType emExchangeType,lpAccount pAcc)
	{
		if(m_RevicedCB!=NULL)
		{
			m_RevicedCB(emExchangeType,pAcc,m_pParent);
		}
	};
	BOOL Init(CString strServerIP,int iPort=9000); 
	BOOL SendNetData(lpCommandInfo pCommandInfo);
	BOOL PrecessProtocal(lpNetExchange pExchange,UDTSOCKET* pUDTSOCKET);
public: 
	BOOL SendData(lpCommandInfo pCommand);
	BOOL Stop();
	BOOL Start(CString strServerIP,int iPort=9000);
	CString GetLastError();
	BOOL IsRunning();
	lpCommandInfo PopTask();
	BOOL PushTask(lpCommandInfo pCommand);
	CClientManager();
	virtual ~CClientManager();
	static CClientManager* CreateInstance();
	void SetRevicedCB(pAccountReviced pFuncRevicedCB,void* pParam)
	{
		m_pParent=pParam;
		m_RevicedCB=pFuncRevicedCB;
	};
	void SetSocketCB(SocketStateChanged pFuncSocketCB,void* pParam)
	{
		m_pParent=pParam;
		m_SocketCB=pFuncSocketCB;
	};
	
protected:
	static DWORD WINAPI RecveDataThread(void* pParam); //Recve Thread
	static DWORD WINAPI SendDataThread(void* pParam); //Recve Thread
};
#endif // !defined(AFX_SERVERMANAGER_H__4A49B5A1_053C_422B_ACBF_31E0091A5EBC__INCLUDED_)
